package com.hatom.cameraxhelp.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

/**
 * Circle 内部圆
 * 带进度条和文字
 * 根据3：4 相机进行圆罩遮挡
 * tangxianfeng
 * 2022.5.18
 */
public class CircleProgressView extends View {

    private Paint bottomCirclePaint;//底部圆画笔
    private float paintWidth=0;//底部圆外扩半径
    private PointF centerPoint;//中心点

    private Paint mInnerPaint;//内环
    private Paint mTextPaint;//文字
    private RectF InnerRectF;//内环 位置

    private Paint mProgressPaint;//内环 进度

    private int progress=0;//进度

    private String progressStartColor="#b3e6fe";//进度渐变开始颜色
    private String progressEndColor="#04adfb";//进度渐变结束颜色

    public void setProgressStartColor(String progressStartColor) {
        this.progressStartColor = progressStartColor;
    }

    public void setProgressEndColor(String progressEndColor) {
        this.progressEndColor = progressEndColor;
    }

    public CircleProgressView(Context context) {
        super(context);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();
        String text="未检测到人脸";
        float angle=(float) progress*330/100;
        if (angle>330){
            angle=330;
        }else if (angle>30){
            text="请保持不动";
        }
        float width=mTextPaint.measureText(text);

        canvas.drawText(text,centerPoint.x-width/2,InnerRectF.top+100,mTextPaint);

        canvas.drawCircle(centerPoint.x, centerPoint.y, (float)(paintWidth*3/2+paintWidth/2),bottomCirclePaint);
        canvas.drawArc(InnerRectF,105,330,false,mInnerPaint);
        canvas.drawArc(InnerRectF,105,angle,false,mProgressPaint);
    }

    /**
     * 初始化画笔
     */
    private void initPaint(){
        if (bottomCirclePaint==null){
            bottomCirclePaint=new Paint();
            bottomCirclePaint.setColor(Color.WHITE);
            bottomCirclePaint.setAntiAlias(true);//抗锯齿
            bottomCirclePaint.setStyle(Paint.Style.STROKE);//不加这个不显示
        }
        if (mInnerPaint==null){
            mInnerPaint=new Paint();
            mInnerPaint.setColor(Color.GRAY);
            mInnerPaint.setAlpha(60);
            //抗锯齿
            mInnerPaint.setAntiAlias(true);
            //画笔宽度
            mInnerPaint.setStrokeWidth(12);
            //防抖动
            mInnerPaint.setDither(true);
            //设置状态
            mInnerPaint.setStyle(Paint.Style.STROKE);
            //头部小圆点
            mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
        }
        if (mTextPaint==null){
            mTextPaint=new Paint();
            mTextPaint.setColor(Color.WHITE);
            mTextPaint.setTextSize(38f);
        }
        if (mProgressPaint==null){
            mProgressPaint=new Paint();
            mProgressPaint.setColor(Color.BLUE);
            //抗锯齿
            mProgressPaint.setAntiAlias(true);
            //画笔宽度
            mProgressPaint.setStrokeWidth(12);
            //防抖动
            mProgressPaint.setDither(true);
            //设置状态
            mProgressPaint.setStyle(Paint.Style.STROKE);
            //头部小圆点
            mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
        }

        if (centerPoint==null){
            centerPoint=new PointF();
        }
        if (paintWidth<1){
            paintWidth=180;
            centerPoint.x=(float) (getWidth()/2);
            centerPoint.y=(float) (getHeight()/2);
            initRectF();
        }
        Log.e("TEST","onMeasure"+paintWidth);
        bottomCirclePaint.setStrokeWidth(paintWidth);
    }

    private void initRectF(){
        if (InnerRectF==null){
            InnerRectF=new RectF();
        }
        float radius=paintWidth*3/2+6;
        InnerRectF.top=centerPoint.y-radius;
        InnerRectF.left=centerPoint.x-radius;
        InnerRectF.bottom=centerPoint.y+radius;
        InnerRectF.right=centerPoint.x+radius;
        LinearGradient lg=new LinearGradient(InnerRectF.left,InnerRectF.top,InnerRectF.right,InnerRectF.bottom,Color.parseColor(progressStartColor),Color.parseColor(progressEndColor), Shader.TileMode.MIRROR);
        mProgressPaint.setShader(lg);

    }

    public void setCenterPoint(float x,float y){

        Log.e("TEST","setCenterPoint"+x+"   "+y);
        if (centerPoint==null){
            centerPoint=new PointF();
        }
        centerPoint.x=x;
        centerPoint.y=y;
        initRectF();
        postInvalidate();
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

    public void setPaintWidth(float paintWidth) {
        Log.e("TEST","setPaintWidth"+paintWidth);
        this.paintWidth = paintWidth;
    }
}
